package com.wcar.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.product.dao.*;
import com.wcar.product.service.ProductService;
import com.wcar.product.dao.ProductReviewImageDao;
import com.wcar.product.vo.ProductVo;
import com.wcar.product.vo.ReviewCountVo;
import com.wcar.product.vo.SelectPeopleAndRatingVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductExamineDao productExamineDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductDao productDao;
    @Resource
    private ProductOrderDao productOrderDao;
    @Autowired
    private StoreUserDao storeUserDao;
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private ProductReviewDao productReviewDao;
    @Autowired
    private StoreProductDao storeProductDao;
    @Autowired
    private ProductReviewImageDao productReviewImageDao;
    @Autowired
    private FrontUserDao frontUserDao;
    @Autowired
    private StoreEvaluateDao storeEvaluateDao;
    @Override
    public Result<Page<Product>> list(ProductVo productVo) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<StoreUser> queryWrapper = new QueryWrapper<>();
        if(user.getUserId()!=1){
            queryWrapper.eq("user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(queryWrapper);
        ArrayList<Integer> storeIds = new ArrayList<>();
        for (StoreUser store:storeUsers){
            storeIds.add(store.getStoreId());
        }
        Page<Product> page = new Page<>(productVo.getCurrentPage(),productVo.getPageSize());
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(productVo.getProductName()!=null&&productVo.getProductName()!=""){
            wrapper.like("product_name",productVo.getProductName());
        }
        wrapper.eq("examine_status","1");
        if (storeIds.size()==0){
            return new Result<>(200,"查询商品列表成功",new Page<>());
        }
        wrapper.in("store_id",storeIds);
        Page<Product> list = productDao.getPage(page, wrapper);
        return new Result<>(200,"查询商品列表成功",list);

    }

    @Override
    public Result<Product> updateProduct(Product product) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        product.setUpdateTime(new Date());
        product.setUpdateId(user.getUserId());
        int i = productDao.updateById(product);
        return new Result<>(200,"修改商品成功",null);
    }

    @Override
    public Result<Product> deleteByProductId(Integer productId) {
        int i = productDao.deleteById(productId);
        return new Result<>(200,"删除商品成功",null);
    }

    @Override
    public Result<Product> insertProduct(Product product) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        product.setCreateId(user.getUserId());
        product.setCreateTime(new Date());
        product.setExamineStatus("0");
        if (product.getIntegral()!=0&&product.getIntegral()!=null){
            product.setIntegralEnabled("0");
        }else {
            product.setIntegralEnabled("1");
        }
        if(product.getFreight()!=new BigDecimal(0.0)&&product.getFreight()!=null){
            product.setIsFreightShipping("1");
        }else{
            product.setIsFreightShipping("0");
        }
        int insert = productDao.insert(product);
        ProductExamine productExamine= new ProductExamine();
        productExamine.setDelStatus("0");
        productExamine.setProductDetails(product.getProductDetails());
        productExamine.setExamineId(user.getUserId());
        productExamine.setFreight(product.getFreight().doubleValue());
        productExamine.setIsFreightShipping(product.getIsFreightShipping());
        productExamine.setIsHistory("0");
        productExamine.setIsline("1");
        productExamine.setPointsenabled(product.getIntegralEnabled());
        productExamine.setName(product.getProductName());
        productExamine.setPicture(product.getPicture());
        productExamine.setPoints(product.getIntegral());
        productExamine.setPrompt(product.getPrompt());
        productExamine.setStoreId(product.getStoreId());
        productExamine.setProTypeId(product.getProTypeId());
        productExamine.setProductId(product.getProductId());
        productExamine.setNewPrice((int) product.getNewPrice().doubleValue());
        productExamine.setLoadPrice((int) product.getLoadPrice().doubleValue());
        productExamineDao.insert(productExamine);
        return new  Result<>(200,"添加商品成功",null);
    }

    @Override
    public Result<List<Product>> selectAllProduct() {
        List<Product> products = productDao.selectList(null);
        return new Result<>(200,"查询商品信息成功",products);
    }

    @Override
    public Result<List<Product>> GetProductByTypeId(String proTypeId, String productName) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_type_id",proTypeId);
        if(productName!=null&&productName!=""){
            wrapper.like("product_name",productName);
        }
        List<Product> products = productDao.selectList(wrapper);
        for (Product product : products){
            QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("store_id",product.getStoreId());
            Store store = storeDao.selectOne(queryWrapper);
            product.setStoreName(store.getStoreName());
        }
        return new Result<>(200,"根据Id查询商品成功",products);
    }

    @Override
    public Result<List<Product>> selectProductAndStoreNameByStoreId(String productName, Integer storeId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("product_name",productName);
        if(storeId!= null){
            wrapper.eq("store_id",storeId);
        }
        List<Product> products = productDao.selectList(wrapper);
        for (Product product : products){
            QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("store_id",product.getStoreId());
            Store store = storeDao.selectOne(queryWrapper);
            product.setStoreName(store.getStoreName());
        }
        return new Result<>(200,"根据Id查询商品成功",products);
    }
    @Override
    public Result<List<Product>> selectProductAndStoreNameByProductName(String productName) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("product_name",productName);
        List<Product> products = productDao.selectList(wrapper);
        for (Product product : products){
            QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("store_id",product.getStoreId());
            Store store = storeDao.selectOne(queryWrapper);
            product.setStoreName(store.getStoreName());
        }
        return new Result<>(200,"根据Id查询商品成功",products);
    }

    @Override
    public Result<SelectPeopleAndRatingVo> selectStoreNameAndRatingByStoreName(String storeName) {
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_name",storeName);
        Store store = storeDao.selectOne(queryWrapper);
        QueryWrapper<StoreEvaluate> wrapper = new QueryWrapper<>();
        wrapper.in("store_id",store.getStoreId());
        List<StoreEvaluate> storeEvaluates = storeEvaluateDao.selectList(wrapper);
        Integer num=0;
        if (storeEvaluates.size()==0){
            return new Result<>(200,"根据Id查询商品成功",new SelectPeopleAndRatingVo(0,0));
        }
        for (StoreEvaluate productReview : storeEvaluates){
            num+=productReview.getStar();
        }
        SelectPeopleAndRatingVo selectPeopleAndRatingVo=new SelectPeopleAndRatingVo(storeEvaluates.size(),num/storeEvaluates.size());
        return new Result<>(200,"根据Id查询商品成功",selectPeopleAndRatingVo);
    }

    @Override
    public Result<List<ProductReviewImage>> selectProductImagesByProductId(Integer productId) {
        QueryWrapper<ProductReview> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        List<ProductReview> productReviews = productReviewDao.selectList(wrapper);
        ArrayList<Integer> reviewIds= new ArrayList<>();
        for (ProductReview productReview : productReviews){
            reviewIds.add(productReview.getId());
        }
        QueryWrapper<ProductReviewImage> queryWrapper = new QueryWrapper<>();
        if(reviewIds.size()==0){
            return new Result<>(200,"根据Id查询商品成功",null);
        }
        queryWrapper.in("review_id",reviewIds);
        List<ProductReviewImage> productReviewImages = productReviewImageDao.selectList(queryWrapper);
        return new Result<>(200,"根据Id查询商品评论图片成功",productReviewImages);
    }

    @Override
    public Result<ReviewCountVo> selectContentByProductId(Integer productId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<ProductReview> productReviews = productReviewDao.selectList(queryWrapper);
        Double num = 0.0;
        if(productReviews.size()==0){
            return new Result<>(200,"根据商品Id查看评价成功",new ReviewCountVo(0,0.0));
        }
        for (ProductReview productReview : productReviews){
            num+=productReview.getRating();
        }
        return new Result<>(200,"根据商品Id查看评价成功",new ReviewCountVo(productReviews.size(),num/(productReviews.size()*5)*100));
    }
    @Override
    public Result<List<ProductReview>> selectContentsByProductId(Integer productId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<ProductReview> productReviews = productReviewDao.selectList(queryWrapper);
        for (ProductReview productReview : productReviews){
            QueryWrapper<FrontUser> userWrapper = new QueryWrapper<>();
            userWrapper.eq("user_id", productReview.getUserId());
            FrontUser frontUser = frontUserDao.selectOne(userWrapper);
            productReview.setPhone(frontUser.getPhone());
        }
        return new Result<>(200,"根据商品Id查看评价成功",productReviews);
    }

    @Override
    public Result<ProductReview> insertProductOrder(ProductOrder productOrder) {
        productOrder.setCreateTime(new Date());
        productOrderDao.insert(productOrder);
        return null;
    }
}
